package com.esun.cheetah.view.setting.connect;

import com.esun.cheetah.view.setting.service.NotificationService;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * A broadcast receiver to handle the changes in network connectiion states.
 * 
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public class ConnectivityReceiver extends BroadcastReceiver {

	private NotificationService notificationService;

	public ConnectivityReceiver(NotificationService notificationService) {
		this.notificationService = notificationService;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		// NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason:
		// (unspecified), extra: (none), roaming: false, failover: false,
		// isAvailable: true
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

		if (networkInfo != null) {
			if (networkInfo.isConnected()) {
				notificationService.connect();
			}
		} else {
			notificationService.disconnect();
		}
	}

}
